/*
http://en.wikipedia.org/wiki/Data_structure_alignment

Data structure alignment is the way data is arranged and accessed in computer memory. 
It consists of two separate but related issues: data alignment and data structure padding. 
When modern computers read from or write to a memory address, it will do this in word sized chunks 
(e.g. 4 byte chunks on a 32-bit system). Data alignment is to put the data at a memory offset 
equal to some multiple of the word size, which increases the system's performance due to 
how the CPU handles memory. To align the data, it may be necessary to insert some meaningless bytes 
between the end of the last data structure and the start of the next, which is data structure padding.

For example, when the computer's word size is 4 bytes, the data to be read should be at a memory offset
which is some multiple of 4. When this is not the case, e.g. the data starts at the 14th byte instead of 
the 16th byte, then the computer has to read two 4-byte chunks and do some calculation before the requested 
data has been read, or it may generate an alignment fault. Even though the previous data structure ends at
the 14th byte, the next data structure should start at the 16th byte. Two padding bytes are inserted between
the two data structures to align the next data structure to the 16th byte.

*/

#include <cstdlib>
#include <iostream>
#include <typeinfo>


using namespace std;
//#pragma pack(4)
struct S1{
       char a;
       short b;
       int c;
};

struct S2{
       char a;
       int b;
       short c;
};

struct S3{
       short a;
       short b;
       short c;
};

struct S4{
       char a;
       short b;
       char c;
};

struct S5{
       char a;
       char b;
       char c;
};


template<typename T>
class ShowLayout{
    public:
    static void execute(){
        T s;    
        printf("%s sizeof = %d\n", typeid(s).name(), sizeof(T));
        printf("%x\n",&s.a);
        printf("%x\n",&s.b);
        printf("%x\n",&s.c);  
        printf("==========\n");
    }
};

int main(int argc, char *argv[])
{
    ShowLayout<S1>::execute();
    ShowLayout<S2>::execute();
    ShowLayout<S3>::execute();
    ShowLayout<S4>::execute();
    ShowLayout<S5>::execute();               
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
